﻿//创建时间: 2010-10-29
//创建作者: 李晓忠
//功能说明: CMS系统 页面发布 执行单个任务
//修改: 2010-11-22 李城 完成代码的编写 
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.CMS.Web.UI;
using TRPOP.CMS.Model;
using TRPOP.CMS.Business;
using TRPOP.Common;
using TRPOP.Config;
using Newtonsoft.Json;
using System.Threading;
using System.Collections;
using TRPOP.CMS.Publish;
using TRPOP.CMS.Plugin;

namespace TRPOP.CMS.Web.Admin
{
    public partial class admin_taskexec : BasePage
    {

        #region 页面变量
        private ContentManage m_ContentManage = new ContentManage();
        private static NodeManage mNodeManage = new NodeManage();
        #endregion

        #region 页面事件

        protected void Page_Load(object sender, EventArgs e)
        {           
            if (!IsPostBack)
            {
                //LoadBase();
                ExeQuery();
            }
        }


        #endregion

        #region 页面方法
        /// <summary>
        /// 操作导航
        /// </summary>
        /// <summary>
        public void LoadBase()
        {
            string strNodeids = TRPOP.Common.RequestHelper.RequestString("nodeid").Trim();//操作的结点
            string strContentids = TRPOP.Common.RequestHelper.RequestString("contentid").Trim();//所有操作的内容ids
            string strAcType = TRPOP.Common.RequestHelper.RequestString("ac").Trim();//操作类型
            string strExtras = TRPOP.Common.RequestHelper.RequestString("Extras");
            #region
            int intNodeid = 0;
            NodeInfo oNodeInfo = null;
            int.TryParse(strNodeids, out intNodeid);
            oNodeInfo = mNodeManage.GetInfoById(intNodeid);
            if (oNodeInfo == null)
                oNodeInfo = new NodeInfo();
            #endregion
            JavaScriptObject otasks = new JavaScriptObject();
            //otasks.Add("action", strAcType);
            //otasks.Add("execbln", "false");
            //otasks.Add("execUrl", "");
            //otasks.Add("execMSG", "");
            //otasks.Add("Nodecontinue", "false");
            bool ExecBln = true;
            switch (strAcType)
            {
                case "node":
                    ExecBln = Publish.PublishHtml.PubNode(strNodeids, TRPOP.Common.RequestHelper.RequestInt("index"), TRPOP.Common.RequestHelper.RequestInt("countNum"));
                    //base.ExecuteJs(string.Format(strExecJS, strNodeids, "asd"));
                    otasks.Add("msg", strNodeids);
                    otasks.Add("index", TRPOP.Common.RequestHelper.RequestInt("index"));
                    //otasks["execMSG"] = strNodeids;
                    break;
                case "content":
                    ExecBln = Publish.PublishHtml.PubContent(strContentids);
                    //base.ExecuteJs(string.Format(strExecJS, strNodeids, "asd"));
                    //otasks["execMSG"] = strContentids;
                    otasks.Add("msg", strContentids);
                    break;
                case "Extra":
                    //附加发布暂时不操作
                    ExecBln = Publish.PublishHtml.PubExtra(strExtras, TRPOP.Common.RequestHelper.RequestInt("index"), TRPOP.Common.RequestHelper.RequestInt("countNum"));
                    //base.ExecuteJs(string.Format(strExecJS, strNodeids, "asd"));
                    //otasks["execMSG"] = strExtras;
                    otasks.Add("msg", strExtras);
                    break;
                default:
                    ExecBln = false;
                    otasks.Add("msg", "没有操作");
                    break;
            }
            //otasks["execbln"] = ExecBln.ToString().ToLower();
            otasks.Add("result", ExecBln ? 1 : 0);
            Response.Write(JavaScriptConvert.SerializeObject(otasks));
            base.End();
        }

        /// <summary>
        /// 多线程发布
        /// </summary>
        public void ExeQuery()
        {
            string strAction = TRPOP.Common.RequestHelper.RequestString("ac").Trim();
            if (strAction == "node" || strAction == "content" || strAction == "Extra")
            {
                PubTaskInfo oPubTaskInfo = new PubTaskInfo();
                oPubTaskInfo.dtAddTime = DateTime.Now;
                oPubTaskInfo.intAdminID = (IAdminUserAccess.GetInstance().getUserBaseInfo() as TRPOP.CMS.UserManage.AdminUserInfo).UserBaseId;
                oPubTaskInfo.intCountNum = RequestHelper.RequestInt("countNum");
                oPubTaskInfo.intIndexPage = RequestHelper.RequestInt("index");
                oPubTaskInfo.intNodeID = RequestHelper.RequestInt("nodeid");
                oPubTaskInfo.intTaskStatus = (int)(PubTaskKeys.TaskStatusType.IsNotStarted);
                oPubTaskInfo.nvcActiontype = strAction;
                if (oPubTaskInfo.intCountNum > SYSConfig.SystemVar_listPageCount)
                {
                    oPubTaskInfo.intCountNum = SYSConfig.SystemVar_listPageCount;
                }
                if (oPubTaskInfo.intNodeID > 0)
                {
                    NodeInfo oNodeInfo = mNodeManage.GetInfoById(oPubTaskInfo.intNodeID);
                    if (oNodeInfo != null)
                    {
                        oPubTaskInfo.nvcNodeName = oNodeInfo.Name;
                    }
                }
                if (oPubTaskInfo.nvcNodeName.IsEmpty())
                {
                    oPubTaskInfo.nvcNodeName = "";
                }
                if (strAction == "content")
                {
                    string strResquestContentids = RequestHelper.RequestString("contentid").Trim();
                    if (strResquestContentids.StartsWith("0,"))
                    {
                        strResquestContentids = strResquestContentids.Substring(2);
                    }
                    oPubTaskInfo.nvcContentIDs = strResquestContentids;
                }
                else if (strAction == "Extra")
                {
                    oPubTaskInfo.nvcContentIDs = RequestHelper.RequestString("Extras").Trim();
                }
                else
                    oPubTaskInfo.nvcContentIDs = "";
                PubTaskDAL.Save(oPubTaskInfo);
                JavaScriptObject oInfo = new JavaScriptObject();
                oInfo.Add("pubId", oPubTaskInfo.intPubTaskId);
                oInfo.Add("result", 1);
                new MultiThreadPub().RunTask(oPubTaskInfo);
                Response.Write(JavaScriptConvert.SerializeObject(oInfo));
                base.End();
            }
        }

        /* 之前的用于测试线程发布的例子
        /// <summary>
        /// 操作导航
        /// </summary>
        /// <summary>
        public void ExeQuery()
        {
            string strAction = TRPOP.Common.RequestHelper.RequestString("ac").Trim();
            if (strAction == "node" || strAction == "content" || strAction == "Extra")
            {
                string[] oList = new string[]{
                    TRPOP.Common.RequestHelper.RequestString("nodeid").Trim(),
                    TRPOP.Common.RequestHelper.RequestString("contentid").Trim(),
                    TRPOP.Common.RequestHelper.RequestString("ac").Trim(),
                    TRPOP.Common.RequestHelper.RequestString("Extras").Trim(),
                    TRPOP.Common.RequestHelper.RequestInt("index").ToString(),
                    TRPOP.Common.RequestHelper.RequestInt("countNum").ToString()
                };
                Thread thread = new Thread(new ParameterizedThreadStart(this.ExecMethord));
                thread.Start(oList);
            }
        }
        public  void ExecMethord(object oOBj)
        {
            string[] oList = oOBj as string[];
            string strNodeids = oList[0];//操作的结点
            string strContentids = oList[1];//所有操作的内容ids
            string strAcType = oList[2];//操作类型
            string strExtras = oList[3];//Extras
            int intIndex = int.Parse(oList[4]);//index
            int intcountNum = int.Parse(oList[5]);//countNum
            int intNodeid = 0;
            NodeInfo oNodeInfo = null;
            int.TryParse(strNodeids, out intNodeid);
            oNodeInfo = mNodeManage.GetInfoById(intNodeid);
            if (oNodeInfo == null)
                oNodeInfo = new NodeInfo();
            JavaScriptObject otasks = new JavaScriptObject();
            bool ExecBln = true;
            switch (strAcType)
            {
                case "node":
                    ExecBln = Publish.PublishHtml.PubNode(strNodeids, intIndex, intcountNum);
                    otasks.Add("msg", strNodeids);
                    otasks.Add("index", intIndex);
                    break;
                case "content":
                    ExecBln = Publish.PublishHtml.PubContent(strContentids);
                    otasks.Add("msg", strContentids);
                    break;
                case "Extra":
                    ExecBln = Publish.PublishHtml.PubExtra(strExtras, intIndex,intcountNum);
                    otasks.Add("msg", strExtras);
                    break;
                default:
                    ExecBln = false;
                    otasks.Add("msg", "没有操作");
                    break;
            }
            otasks.Add("result", ExecBln ? 1 : 0);
            //Response.Write(JavaScriptConvert.SerializeObject(otasks));
            //if (cookie.getCookie('mainurl') != "")
            //
            HttpCookie cookie = new HttpCookie("TaskExecStatus");
            cookie.Value = JavaScriptConvert.SerializeObject(otasks);
            if (HttpContext.Current.Request.Url.Host.Contains("."))
                cookie.Domain = HttpContext.Current.Request.Url.Host;
            cookie.Path = "/";
            HttpContext.Current.Response.AppendCookie(cookie);
            base.End();
             // 
        }
        */

        #endregion
    }
}
